# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

head=ListNode(1)
head.next=ListNode(2)
head.next.next=ListNode(6)
head.next.next.next=ListNode(3)
head.next.next.next.next=ListNode(4)
head.next.next.next.next.next=ListNode(5)
head.next.next.next.next.next.next=ListNode(6)
val=6
def removeElements(head: ListNode, val: int):
    if head==None:
        return None
    #做一个前置的头结点
    headNew=ListNode()
    headNew.next=head
    headPro=headNew
    while headPro and head:
        if head.val==val:
            #要删除当前节点
            headPro.next=head.next
            head=headPro.next
        else:
            #不删除的情况
            headPro=headPro.next
            head=head.next

    while headNew:
        print(headNew.val)
        headNew=headNew.next
removeElements(head,val)
